package mypackage;

import java.io.IOException;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import samplecode.MyConnectionFactory;
import net.rim.blackberry.api.browser.URLEncodedPostData;
import net.rim.device.api.browser.field2.BrowserFieldConfig;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;

/**
 * This class extends the UiApplication class, providing a
 * graphical user interface.
 */
public class MyApp extends UiApplication
{
    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */ 
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        MyApp theApp = new MyApp();       
        theApp.enterEventDispatcher();
    }
    

    /**
     * Creates a new MyApp object
     */
    public MyApp()
    {        
        // Push a screen onto the UI stack for rendering.
        //pushScreen(new MyScreen());
    	xs();
    }    
    MenuItem m1 = new MenuItem("Login", 0, 0) {
        public void run() {
        	HttpConnection hc = null;
    		try {
    			Dialog.alert("paso");
    			hc = (HttpConnection)Connector.open("127.0.0.1:8888/restWS/resources/factorial?numero=3"+ConnectionString.getConnectionString());
    			//URLEncodedPostData oPostData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
    			//oPostData.append("lat", "100");
    			//oPostData.append("long", "200");
    			OutputStream strmOut = hc.openOutputStream();
    			//strmOut.write(oPostData.getBytes());
    			strmOut.flush();
    			strmOut.close();
    			//int blah = hc.getResponseCode();
    			String returnMessage = hc.getResponseMessage();
    			Dialog.alert(returnMessage);
    		}

    		catch (IOException ioe) {

    		} 
    		finally {
    			try { 
    				if (hc != null) hc.close();
    			} catch (IOException ignored) {}
    		}
        }
    };
    /**
     * 
     */
    public void xs(){
    	MyConnectionFactory _factory = new MyConnectionFactory();

    	BrowserFieldConfig _config = new BrowserFieldConfig();
    	_config.setProperty(BrowserFieldConfig.CONNECTION_FACTORY, _factory);

    	//BrowserField _bf2 = new BrowserField(_config);

    	MainScreen _screen = new MainScreen();
    	_screen.addMenuItem(m1);
    	_screen.add(new LabelField("s: "+ConnectionString.getConnectionString()));
    	
    	//_screen.add(_bf2);
    	pushScreen(_screen);
    	
    	//_bf2.requestContent("http://devblog.blackberry.com");
    }
}
